<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-03-03 09:25:35
 * ------------------------------------------------ *
 * 用户管理                                   *
 * ------------------------------------------------ */
namespace Admin\Model;


class UsersModel extends CommonModel{

// 新增
	 public function addUsers(){

	 	//检测账号
	 	$hasLoginName = $this->checkLoginKey(I("loginName"));

	    if(!$hasLoginName){
	 		return false;
	 	}

	 	if(I("userPhone")!=''){
		 	$hasUserPhone = $this->checkLoginKey(I("userPhone"));
		 	if(!$hasUserPhone){
		 		return false;
		 	}
	 	}

	 	if(I("userEmail")!=''){
		 	$hasUserEmail = $this->checkLoginKey(I("userEmail"));
		 	if(!$hasUserEmail){
		 		return false;
		 	}
	 	}
	 	
	 	//创建数据
	 	$id = I("id",0);
		$data = array();
		$data["loginName"] = I("loginName");
		$data["loginSecret"] = rand(1000,9999);
		$data["loginPwd"] = md5(I('loginPwd').$data['loginSecret']);
		if($this->checkEmpty($data)){
			//$data["userPhoto"] = I("userPhoto");

			$data["userPhoto"] = 'avatar/avatar.jpg';
	    	if(!empty($_FILES['userPhoto']['tmp_name'])){
				$userPhoto=$this->upAvatar('userPhoto');
				if($userPhoto==false){
					return false;
				}
				$data["userPhoto"] = $userPhoto;
			}

			$data["userName"] = I("userName");
			$data["userStatus"] = I("userStatus",0);
			$data["userType"] = (int)I("userType",0);
			$data["userSex"] = (int)I("userSex",0);
			$data["userEmail"] = I("userEmail");
			$data["userPhone"] = I("userPhone");
			$data["userQQ"] = I("userQQ");
			$data["userScore"] = I("userScore",0);
		    $data["userTotalScore"] = I("userTotalScore",0);
		    $data["userFlag"] = 1;
		    $data["userStatus"] = I('userStatus');
		    $data["createTime"] = date('Y-m-d H:i:s');
			$m = M('users');
			$rs = $m->add($data);
			if(false !== $rs){
				return true;
			}

			if(!empty($userPhoto)){
				$this->delAvatar($userPhoto);
			}
		}
		return false;
	 } 
 
// 修改

	 public function editUsers(){
	 	$id = (int)I('id',0);
	 	//return '8888888888';
	 	//检测账号
	 	if(I("userPhone")!=''){
	        $hasUserPhone = $this->checkLoginKey(I("userPhone"),$id);
	        if(!$hasUserPhone){		        
		 		return false;
	        }
	 	}
	 	if(I("userEmail")!=''){
		 	$hasUserEmail = $this->checkLoginKey(I("userEmail"),$id);
		 	if(!$hasUserEmail){
		 		return false;
		 	}
		 	
	 	}

	 	//修改数据
		$m = M('users');
		$oldUserPhoto=$m->where('userId='.$id)->getField('userPhoto');
		$data = array();
		$data["userScore"] = (int)I("userScore",0);
		$data["userTotalScore"] = (int)I("userTotalScore",0);
		if($this->checkEmpty($data)){	
			$data["userName"] = I("userName");
			//$data["userPhoto"] = I("userPhoto");

			//$data["userPhoto"] = 'avatar/avatar.jpg';
	    	if(!empty($_FILES['userPhoto']['tmp_name'])){
				$userPhoto=$this->upAvatar('userPhoto');
				if($userPhoto==false){
					return false;
				}
				$data["userPhoto"] = $userPhoto;
			}
			$data["userStatus"] = I('userStatus');
			$data["userSex"] = (int)I("userSex",0);
		    $data["userQQ"] = I("userQQ");
		    $data["userPhone"] = I("userPhone");
		    $data["userEmail"] = I("userEmail");
			$rs = $m->where("userId=".$id)->save($data);
			if(false !== $rs){
				if(!empty($userPhoto) && $oldUserPhoto!='avatar/avatar.jpg'){
					$this->delAvatar($oldUserPhoto);
				}

				return true;
				
			}

			if(!empty($userPhoto)){
				$this->delAvatar($userPhoto);
			}
		}
		return false;
	 } 

// 获取指定用户

     public function get(){
	 	$m = M('users');
		return $m->where("userId=".(int)I('id'))->find();
	 }

// 分页列表

     public function getUsers(){
        $m = M('users');
        $map = array();
	 	$sql = "select * from __PREFIX__users where userFlag=1 ";
	 	if(I('loginName')!='')$sql.=" and loginName LIKE '%".I('loginName')."%'";
	 	if(I('userPhone')!='')$sql.=" and userPhone LIKE '%".I('userPhone')."%'";
	 	if(I('userEmail')!='')$sql.=" and userEmail LIKE '%".I('userEmail')."%'";
	 	if(I('userType',-1)!=-1)$sql.=" and userType=".I('userType',-1);
	 	$sql.="  order by userId desc";
		$rs = $this->PQ($sql,0,$pageSize);
		//计算等级
		if(count($rs)>0){
			$m = M('user_ranks');
			$urs = $m->select();
			foreach ($rs['list'] as $key=>$v){
				foreach ($urs as $rkey=>$rv){
					if($v['userTotalScore']>=$rv['startScore'] && $v['userTotalScore']<$rv['endScore']){
					   $rs['list'][$key]['userRank'] = $rv['rankName'];
					}
				}
			}
		}
		return $rs;
	 }

// 删除

	 public function delUsers(){
	 	
	 	$id = (int)I('id');
	 	$m = M('users');
	 	//获取用户类型
	 	$userType = $m->where('userId='.$id)->getField('userType',1);
	 	$m->userFlag = -1;
		$rs = $m->where(" userId=".$id)->save();
		if(false !== $rs){
		   //如果是商家还要下架他的商品
		   if($userType==1){
		   	    $m = M('shops');
		   	    $m->shopFlag = -1;
		   	    $m->shopStatus=-2;
		   	    $rs = $m->where(" userId=".$id)->save();
		   	    $shopId = $m->where('userId='.$id)->getField('shopId',1);
				$sql = "update __PREFIX__goods set isSale=0,goodsStatus=-1 where shopId=".$shopId;
			 	$m->execute($sql);
		   }
		   return true;
		}
		
		return false;
	 }

// 查询登录

	 public function checkLoginKey($val,$id = 0){	 	
	 	if($val=='')return false;
	 	$sql = " (loginName ='%s' or userPhone ='%s' or userEmail='%s') and userFlag=1";
	 	$keyArr = array($val,$val,$val);
	 	if($id>0){
	 		$sql.=" and userId!=".$id;
	 	}
	 	$m = M('users');
	 	$rs = $m->where($sql,$keyArr)->count();
	    if($rs>0) return false;
	    return true;
	 }
 

// 编辑账号状态

	 public function editUserStatus(){
	 	if(I('id',0)==0)return false;
	 	$m = M('users');
	 	$m->userStatus = (I('userStatus')==1)?0:1;
	 	$rs = $m->where("userId=".I('id',0))->save();
	    if(false !== $rs){
			return true;
		}
	 	return false;
	 }


//以下前台备用方法
// 获取账号信息

	 public function getAccount(){
	 	 $m = M('users');
		 $rs = $m->where('userId='.I('id',0))->getField('userId,loginName,userStatus,userType',1);
		 return current($rs);
	 }

// 修改账号信息

	 public function editAccount(){
	 	if(I('id')=='')return flase;
	 	$m = M('users');
	 	$loginSecret = $m->where("userId=".I('id'))->getField('loginSecret');
	 	if(I('loginPwd')!='')$m->loginPwd = md5(I('loginPwd').$loginSecret);
	 	$m->userStatus = I('userStatus',0);
	 	$rs = $m->where('userId='.(int)I('id'))->save();
	    if(false !== $rs){
			return true;
		}
		return false;
	 }
}